# -*- coding: utf-8 -*-
"""
@author: hunan
@date: 2024-12-08
@description: 
"""
import json
import socket
from functools import wraps

from apscheduler.schedulers.background import BackgroundScheduler
from apscheduler.triggers.cron import CronTrigger

from product_upload.core.amazon_us import gen_amazon_to_xlsm
from product_upload.domain.amazon_us.amazon_us_upload import AmazonUsUpload
from product_upload.util.basic.common_util import sleep_random_duration
from product_upload.util.basic.mysql_util import db_batch_update, get_amazon_us_upload_and_lock_task
from product_upload.util.supplier.doba_util import db_flush_hand_time_and_not_available
from product_upload.util.supplier.saleyee_util import sy_flash_not_available

scheduler = BackgroundScheduler()


# 定义主机名检查装饰器
def scheduled_task(cron_expression: str, allowed_hosts=("center", "admin_pro", "dev_3070", "hunan", "robot1", "robot2", "robot3", "robot4", "robot5", "robot6", "robot7", "robot8", "robot9", "tmp", "admin","robot53")):
    def decorator(func):
        @wraps(func)
        def wrapper(*args, **kwargs):
            current_host = socket.gethostname().lower()
            if current_host in allowed_hosts:
                func(*args, **kwargs)

        current_host = socket.gethostname().lower()
        if current_host in allowed_hosts:
            cron_trigger = CronTrigger.from_crontab(cron_expression)
            scheduler.add_job(func, trigger=cron_trigger, id=func.__name__)
        return wrapper

    return decorator


# 每五分钟执行一次制作表格的任务
@scheduled_task("*/5 * * * *", allowed_hosts=("center", "robot6", "robot7", "robot8", "robot9", "robot53"))
def gen_xlsm_task():
    # 当有程序在查询,则会锁住线程
    sleep_random_duration(1, 5)
    process_core = 42
    task = get_amazon_us_upload_and_lock_task(",".join(AmazonUsUpload.fields), AmazonUsUpload)
    if task:
        print(f'定时任务开始跑了,任务ID:{task.id}')
        params = json.loads(task.remark)
        opt_name = params.get("opt_name", "")
        brand = params.get("brand", "")
        brand_zip = params.get("brand_zip", "")
        shop = params.get("shop", "")
        not_class_name_list = params.get("not_class_name_list", [])
        platform = params.get("platform", "")
        price = params.get("price", 30)
        if not price:
            price = 30
        limit_img = params.get("limit_img", 3)
        if limit_img < 1:
            limit_img = 1
        if task.upload_limit and task.upload_limit >= 10000:
            task.upload_limit = 9999
        db_batch_update("amazon_us_upload", ["id", "status"], [[task.id, 3]])
        gen_amazon_to_xlsm(opt_name, brand, not_class_name_list, platform, brand_zip, shop, task.id, task.upload_limit, limit_img, price, process_core)

# 每天下午一点 执行
@scheduled_task("0 13 * * *", allowed_hosts="admin_pro")
def sy_flash_not_available_daily():
    sy_flash_not_available()


# 每天上午11点执行dubo 维护商品信息
@scheduled_task("0 11 * * *", allowed_hosts="admin_pro")
def db_flash_not_available_daily():
    db_flush_hand_time_and_not_available()




if __name__ == "__main__":
    pass
